import requests
import logging

# 日志设置
logger = logging.getLogger("记录器（logger）")       # 创建日志记录器对象
logger.setLevel(logging.DEBUG)      # 设置记录器的能记录的最低等级

fhandle = logging.FileHandler("猫猫日志.log", encoding="utf-8")     # 创建日志处理器对象 文件处理器 存放日志
fhandle.setLevel(logging.DEBUG)     # 设置处理器能处理的最低等级

format = '%(asctime)s [%(name)s] [%(filename)s] [line:%(lineno)d] %(levelname)s -- %(message)s'      # 设置记录日志的格式器
set_format = logging.Formatter(format)      # 创建格式器对象
fhandle.setFormatter(set_format)        # 给处理器添加格式器

logger.addHandler(fhandle)      # 给记录器添加处理器

url = "https://day-m.qxdaojia.com/api/excellentmall/corp/media/upload/image?applicationName=QXTECH-QWMANAGER"
file_path = r"C:\Users\Administrator\Desktop\图片\1png.png"

payload = {}
files = [
    ('file', ('1png.png', open(file_path, 'rb'), 'image/png'))
]
headers = {}

response = requests.request("POST", url, headers=headers, data=payload, files=files)
if response.status_code == 200:
    logger.info(f"用户端请求路径：{url}")
    logger.info(f"用户端请求参数：{files}")
    logger.info(f"响应结果：{response.json()}")

print(f"请求路径：{url}")
print(f"上传图片转换接口的请求响应code：{response.status_code}")
print(f"上传图片转换接口的请求响应内容：{response.json()}")


# 参数是表单
eva = "https://day.ddm.qxdaojia.com"
path = '/api/file/oss/uploadFileToSsoEncrypt'  # 单多多上传图片转换接口
url4 = eva + path
file_path = r'C:\Users\Administrator\Desktop\图片\0-正.jpg'  # 原始字符串写法
with open(file_path, 'rb') as f:
    files = {
        'fileDir': 'real/ddd',  # 非文件字段
        'files': ('0-正.jpg', f, 'image/jpeg')   # 文件字段
    }
    headers = {
        'cookie': 'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221105186335603294208%22%2C%22first_id%22%3A%22195188ae01315a3-0ff218050e89cc-4201541c-332800-195188ae014111a%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTk1MTg4YWUwMTMxNWEzLTBmZjIxODA1MGU4OWNjLTQyMDE1NDFjLTMzMjgwMC0xOTUxODhhZTAxNDExMWEiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIxMTA1MTg2MzM1NjAzMjk0MjA4In0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%221105186335603294208%22%7D%2C%22%24device_id%22%3A%22195188ae01315a3-0ff218050e89cc-4201541c-332800-195188ae014111a%22%7D; token=7b4580f2-978b-43f8-8ec4-7275e9f7a40d'
    }

response_4 = requests.post(url4, data=files, headers=headers)
if response_4.status_code != 200:
    logger.info(f"请求路径：{url4}")
    logger.info(f"请求参数：{files}")
    logger.error(f"接口请求失败！code = {response_4.status_code}")
    logger.error(f"接口响应内容：{response_4.json()}")
print(f"请求路径：{url4}")
print(f"上传图片转换接口的请求响应code：{response_4.status_code}")
print(f"上传图片转换接口的请求响应内容：{response_4.json()}")
